<?php
	function validateUsername($username, $password){
		if(strlen($username)<5){
			return 1;
		} else if ($username==$password){
			return 2;
		} else {
			require_once("db.php");
			if(connect_db()){
				$tbl_name="pengguna"; // Table name

				$sql="SELECT * FROM $tbl_name WHERE username='$username'";
				$result=mysql_query($sql);

				$count=mysql_num_rows($result);

				if($count==1)
					return 3;
				else
					return 0;
			}
		}
	}
	
	function validatePassword($password, $username, $email){
		if(strlen($password)<8)
			return 1;
		else if($password==$username)
			return 2;
		else if($password==$email)
			return 3;
		else
			return 0;
	}
	
	function validateConfirm($confirm, $password){
		if($confirm != $password)
			return 1;
		else
			return 0;
	}
	
	function validateFullname($fullname){
		if(!preg_match("/^([a-zA-Z])+ ([a-zA-Z])+$/", $fullname))
			return 1;
		else
			return 0;
	}	
	
	function validateBirthdate($birthdate){
		if(preg_match("/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/", $birthdate)){
			$array_birthdate = explode("-", $birthdate);
			if(!checkdate($array_birthdate[1],$array_birthdate[2],$array_birthdate[0]))
				return 2;
			else
				return 0;
		} else {
			return 1;
		}
	}
	
	function validateEmail($email,$password){
		if($email==$password)
			return 1;
		else if(!preg_match("/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,}$/",$email))			
			return 2;
		else {
			require_once("db.php");
			if(connect_db()){
				$tbl_name="pengguna"; // Table name

				$sql="SELECT * FROM $tbl_name WHERE email='$email'";
				$result=mysql_query($sql);

				$count=mysql_num_rows($result);

				if($count>0)
					return 3;
				else
					return 0;
			}
		}	
	}
	
	function validateAvatar($type){
		if($type!='image/jpeg')
			return 1;
		else
			return 0;
	}	
?>
